package com.sqfw.project.wp.service.impl;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.common.utils.spring.SpringUtils;
import com.sqfw.framework.web.domain.TreeSelect;
import com.sqfw.project.system.domain.SysDept;
import com.sqfw.project.wp.domain.WpTreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wp.mapper.SysCodeMapper;
import com.sqfw.project.wp.domain.SysCode;
import com.sqfw.project.wp.service.ISysCodeService;
import org.springframework.util.CollectionUtils;

/**
 * 工单字典表Service业务层处理
 *
 * @author lx
 * @date 2023-06-07
 */
@Service
public class SysCodeServiceImpl implements ISysCodeService {
    @Autowired
    private SysCodeMapper sysCodeMapper;

    private static final Long codePid = 13552L;

    private static final Long otherCodeid = 14478L;

    /**
     * 查询工单字典表
     *
     * @param autoId 工单字典表主键
     * @return 工单字典表
     */
    @Override
    public SysCode selectSysCodeByAutoId(Long autoId) {
        return sysCodeMapper.selectSysCodeByAutoId(autoId);
    }

    /**
     * 查询工单字典表列表
     *
     * @param sysCode 工单字典表
     * @return 工单字典表
     */
    @Override
    public List<SysCode> selectSysCodeList(SysCode sysCode) {


        return sysCodeMapper.selectSysCodeList(sysCode);
    }


    /**
     * 新增工单字典表
     *
     * @param sysCode 工单字典表
     * @return 结果
     */
    @Override
    public int insertSysCode(SysCode sysCode) {
        return sysCodeMapper.insertSysCode(sysCode);
    }

    /**
     * 修改工单字典表
     *
     * @param sysCode 工单字典表
     * @return 结果
     */
    @Override
    public int updateSysCode(SysCode sysCode) {
        return sysCodeMapper.updateSysCode(sysCode);
    }

    /**
     * 批量删除工单字典表
     *
     * @param autoIds 需要删除的工单字典表主键
     * @return 结果
     */
    @Override
    public int deleteSysCodeByAutoIds(Long[] autoIds) {
        return sysCodeMapper.deleteSysCodeByAutoIds(autoIds);
    }

    /**
     * 删除工单字典表信息
     *
     * @param autoId 工单字典表主键
     * @return 结果
     */
    @Override
    public int deleteSysCodeByAutoId(Long autoId) {
        return sysCodeMapper.deleteSysCodeByAutoId(autoId);
    }

    @Override
    public List<WpTreeSelect> selectSysCodeTreeList(SysCode sysCode) {

        List<SysCode> codes = new ArrayList<>();

        //n为可用，d为删除
        sysCode.setState("n");
        sysCode.setPid(codePid);
        sysCode.setNodetype("class1");
        List<SysCode> class1List = sysCodeMapper.selectSysCodeList(sysCode);
        //除去  内容分类中的  社区生活
        List<SysCode> class1s = class1List.stream()
                .filter(sysCodeCon -> !("社区生活".equals(sysCodeCon.getNodename())))
                .collect(Collectors.toList());
        class1s.forEach(codeCon -> {
            if ("民政业务".equals(codeCon.getNodename())) {
                codeCon.setWeight(1);
            } else if ("政务导航".equals(codeCon.getNodename())) {
                codeCon.setWeight(2);
            } else if ("其他".equals(codeCon.getNodename())) {
                codeCon.setWeight(3);
            }
        });
        List<SysCode> sysCodes = class1s.stream().sorted(Comparator.comparing(SysCode::getWeight)).collect(Collectors.toList());
        codes.addAll(sysCodes);
        List<Long> class2Pids = class1s.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        class2Pids.remove(otherCodeid);
        //查询class2
        SysCode codeClass2 = new SysCode();
        codeClass2.setPids(class2Pids);
        codeClass2.setNodetype("class2");
        codeClass2.setState("n");

        //查询class2类型的code
        List<SysCode> class2List = sysCodeMapper.selectSysCodeListByPids(codeClass2);

        List<Long> longList = new ArrayList<>();
        longList.add(otherCodeid);
        SysCode otherClass2 = new SysCode();
        otherClass2.setPids(longList);
        otherClass2.setNodetype("class2");
        otherClass2.setState("n");
        List<SysCode> otherclass2List = sysCodeMapper.selectSysCodeListByPids(otherClass2);
        otherclass2List.forEach(codeCon -> {
            if ("12345来电".equals(codeCon.getNodename())) {
                codeCon.setWeight(1);
            } else if ("用户跟踪".equals(codeCon.getNodename())) {
                codeCon.setWeight(2);
            } else if ("投诉话务员".equals(codeCon.getNodename())) {
                codeCon.setWeight(3);
            } else if ("办公总机".equals(codeCon.getNodename())) {
                codeCon.setWeight(4);
            } else if ("表扬".equals(codeCon.getNodename())) {
                codeCon.setWeight(5);
            } else if ("通话异常".equals(codeCon.getNodename())) {
                codeCon.setWeight(6);
            } else if ("紧急类来电".equals(codeCon.getNodename())) {
                codeCon.setWeight(7);
            } else if ("其他".equals(codeCon.getNodename())) {
                codeCon.setWeight(8);
            }
        });
        List<SysCode> sysCodes2List = otherclass2List.stream().sorted(Comparator.comparing(SysCode::getWeight)).collect(Collectors.toList());

        codes.addAll(sysCodes2List);
        codes.addAll(class2List);
        List<Long> class3Pids = sysCodes2List.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<Long> collect = class2List.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        class3Pids.addAll(collect);
        //查询class3
        SysCode codeClass3 = new SysCode();
        codeClass3.setPids(class3Pids);
        codeClass3.setNodetype("class3");
        codeClass3.setState("n");
        List<SysCode> class3List = sysCodeMapper.selectSysCodeListByPids(codeClass3);
        codes.addAll(class3List);

        List<Long> class4Pids = class3List.stream().map(SysCode::getAutoId).collect(Collectors.toList());

        //查询class4
        SysCode codeClass4 = new SysCode();
        codeClass4.setPids(class4Pids);
        codeClass4.setNodetype("class4");
        codeClass4.setState("n");
        List<SysCode> class4List = sysCodeMapper.selectSysCodeListByPids(codeClass4);
        codes.addAll(class4List);

        return buildSysCodeTreeSelect(codes);
    }

    @Override
    public List<WpTreeSelect> selectSysCodeListByNodetypes(SysCode sysCode) {
        List<SysCode> codes = sysCodeMapper.selectSysCodeListByNodetypes(sysCode);
        return buildSysCodeTreeSelect(codes);
    }

    @Override
    public List<SysCode> selectSysCodeListToBusiness(SysCode sysCode) {

        List<SysCode> codes = new ArrayList<>();

        //n为可用，d为删除
        sysCode.setState("n");
        sysCode.setPid(codePid);
        sysCode.setNodetype("class1");
        List<SysCode> class1List = sysCodeMapper.selectSysCodeList(sysCode);
        //除去  内容分类中的  社区生活
        List<SysCode> class1s = class1List.stream()
                .filter(sysCodeCon -> !("社区生活".equals(sysCodeCon.getNodename())))
                .collect(Collectors.toList());
        class1s.forEach(codeCon -> {
            if ("民政业务".equals(codeCon.getNodename())) {
                codeCon.setWeight(1);
            } else if ("政务导航".equals(codeCon.getNodename())) {
                codeCon.setWeight(2);
            } else if ("其他".equals(codeCon.getNodename())) {
                codeCon.setWeight(3);
            }
        });
        List<SysCode> sysCodes = class1s.stream().sorted(Comparator.comparing(SysCode::getWeight)).collect(Collectors.toList());
        codes.addAll(sysCodes);
        List<Long> class2Pids = class1s.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        //查询class2
        SysCode codeClass2 = new SysCode();
        codeClass2.setPids(class2Pids);
        codeClass2.setNodetype("class2");
        codeClass2.setState("n");

        //查询class2类型的code
        List<SysCode> class2List = sysCodeMapper.selectSysCodeListByPids(codeClass2);
        codes.addAll(class2List);
        List<Long> class3Pids = class2List.stream().map(SysCode::getAutoId).collect(Collectors.toList());

        //查询class3
        SysCode codeClass3 = new SysCode();
        codeClass3.setPids(class3Pids);
        codeClass3.setNodetype("class3");
        codeClass3.setState("n");
        List<SysCode> class3List = sysCodeMapper.selectSysCodeListByPids(codeClass3);
        codes.addAll(class3List);

        List<Long> class4Pids = class3List.stream().map(SysCode::getAutoId).collect(Collectors.toList());

        //查询class4
        SysCode codeClass4 = new SysCode();
        codeClass4.setPids(class4Pids);
        codeClass4.setNodetype("class4");
        codeClass4.setState("n");
        List<SysCode> class4List = sysCodeMapper.selectSysCodeListByPids(codeClass4);
        codes.addAll(class4List);
        return codes;
    }

    public List<WpTreeSelect> buildSysCodeTreeSelect(List<SysCode> codes) {
        List<SysCode> deptTrees = buildSysCodeTree(codes);
        return deptTrees.stream().map(WpTreeSelect::new).collect(Collectors.toList());
    }

    public List<SysCode> buildSysCodeTree(List<SysCode> codes) {
        List<SysCode> returnList = new ArrayList<SysCode>();
        List<Long> tempList = codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        for (SysCode code : codes) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(code.getPid())) {
                recursionFn(codes, code);
                returnList.add(code);
            }
        }
        if (returnList.isEmpty()) {
            returnList = codes;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysCode> list, SysCode code) {
        // 得到子节点列表
        List<SysCode> childList = getChildList(list, code);
        code.setChildren(childList);
        for (SysCode tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysCode> list, SysCode code) {
        return getChildList(list, code).size() > 0 ? true : false;
    }

    /**
     * 得到子节点列表
     */
    private List<SysCode> getChildList(List<SysCode> list, SysCode code) {
        List<SysCode> tlist = new ArrayList<SysCode>();
        Iterator<SysCode> it = list.iterator();
        while (it.hasNext()) {
            SysCode n = (SysCode) it.next();
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().longValue() == code.getAutoId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }


    @Override
    public List<SysCode> selectSysCodeTreeListByDetail(SysCode sysCode) {

        List<SysCode> codes = new ArrayList<>();

        //n为可用，d为删除
        sysCode.setState("n");
        sysCode.setPid(codePid);
        sysCode.setNodetype("class1");
        List<SysCode> class1List = sysCodeMapper.selectSysCodeList(sysCode);
        //除去  内容分类中的  社区生活
        List<SysCode> class1s = class1List.stream()
                .filter(sysCodeCon -> !("社区生活".equals(sysCodeCon.getNodename())))
                .collect(Collectors.toList());

        codes.addAll(class1s);
        List<Long> class2Pids = class1s.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        class2Pids.remove(otherCodeid);
        //查询class2
        SysCode codeClass2 = new SysCode();
        codeClass2.setPids(class2Pids);
        codeClass2.setNodetype("class2");
        codeClass2.setState("n");

        //查询class2类型的code
        List<SysCode> class2List = sysCodeMapper.selectSysCodeListByPids(codeClass2);

        List<Long> longList = new ArrayList<>();
        longList.add(otherCodeid);
        SysCode otherClass2 = new SysCode();
        otherClass2.setPids(longList);
        otherClass2.setNodetype("class2");
        otherClass2.setState("n");
        List<SysCode> otherclass2List = sysCodeMapper.selectSysCodeListByPids(otherClass2);


        codes.addAll(otherclass2List);
        codes.addAll(class2List);
        List<Long> class3Pids = otherclass2List.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<Long> collect = class2List.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        class3Pids.addAll(collect);
        //查询class3
        SysCode codeClass3 = new SysCode();
        codeClass3.setPids(class3Pids);
        codeClass3.setNodetype("class3");
        codeClass3.setState("n");
        List<SysCode> class3List = sysCodeMapper.selectSysCodeListByPids(codeClass3);
        codes.addAll(class3List);

        List<Long> class4Pids = class3List.stream().map(SysCode::getAutoId).collect(Collectors.toList());

        //查询class4
        SysCode codeClass4 = new SysCode();
        codeClass4.setPids(class4Pids);
        codeClass4.setNodetype("class4");
        codeClass4.setState("n");
        List<SysCode> class4List = sysCodeMapper.selectSysCodeListByPids(codeClass4);
        codes.addAll(class4List);


        return codes;
    }

    @Override
    public List<SysCode> selectSysCodeClass1List(String nodetype,List<Long> pids) {

        SysCode sysCode = new SysCode();
        //n为可用，d为删除
        sysCode.setState("n");
        if ("class1".equals(nodetype)){
            sysCode.setPid(codePid);
            sysCode.setNodetype(nodetype);

        }else {
            sysCode.setNodetype(nodetype);
            sysCode.setPids(pids);
        }
        List<SysCode> codes = sysCodeMapper.selectSysCodeList(sysCode);

        if ("class1".equals(nodetype)){
            //除去  内容分类中的  社区生活
            return codes.stream()
                    .filter(sysCodeCon -> !("社区生活".equals(sysCodeCon.getNodename())))
                    .collect(Collectors.toList());
        }

        return codes;
    }

    @Override
    public List<WpTreeSelect> getSpecificCodeTree() {
        List<SysCode> codes = new ArrayList<>();

        SysCode sysCode = new SysCode();
        //n为可用，d为删除
        sysCode.setState("n");
        sysCode.setPid(codePid);
        sysCode.setNodetype("class1");
        List<SysCode> class1List = sysCodeMapper.selectSysCodeList(sysCode);
        // 民政业务
        List<SysCode> class1s = class1List.stream()
                .filter(sysCodeCon -> ("民政业务".equals(sysCodeCon.getNodename())))
                .collect(Collectors.toList());
        codes.addAll(class1s);
        List<Long> class2Pids = class1s.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        //查询class2
        SysCode codeClass2 = new SysCode();
        codeClass2.setPids(class2Pids);
        codeClass2.setNodetype("class2");
        codeClass2.setState("n");

        //查询class2类型的code
        List<SysCode> class2List = sysCodeMapper.selectSysCodeListByPids(codeClass2);

        // 殡葬管理
        List<SysCode> class2s = class2List.stream()
                .filter(sysCodeCon -> ("殡葬管理".equals(sysCodeCon.getNodename())))
                .collect(Collectors.toList());
        codes.addAll(class2s);
        List<Long> class3Pids = class2s.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        //查询class3
        SysCode codeClass3 = new SysCode();
        codeClass3.setPids(class3Pids);
        codeClass3.setNodetype("class3");
        codeClass3.setState("n");
        List<SysCode> class3List = sysCodeMapper.selectSysCodeListByPids(codeClass3);
        codes.addAll(class3List);

        List<Long> class4Pids = class3List.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(class4Pids)) {
            //查询class4
            SysCode codeClass4 = new SysCode();
            codeClass4.setPids(class4Pids);
            codeClass4.setNodetype("class4");
            codeClass4.setState("n");
            List<SysCode> class4List = sysCodeMapper.selectSysCodeListByPids(codeClass4);
            codes.addAll(class4List);
        }

        return buildSysCodeTreeSelect(codes);
    }
}
